package com.rhythm.commons.security;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.IllegalBlockSizeException;

/**
 *
 * @author Michael J. Lee @ Synergy Energy Holdings, LLC
 */
public class AESCrypter
{

    private Cipher ecipher;

    private Cipher dcipher;

    public AESCrypter(SecretKey key) {
        // Create an 8-byte initialization vector
        byte[] iv = new byte[]
        {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
        };

        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
        try
        {
            ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            // CBC requires an initialization vector
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[1024];

    public void encrypt(InputStream in, OutputStream out) throws IOException {
        try
        {
            out = new CipherOutputStream(out, ecipher);

            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0)
            {
                out.write(buf, 0, numRead);
            }
        } finally
        {
            if (out != null)
                out.close();

            if (in != null)
                in.close();
        }
    }

    public String encrypt(String str) {
        try
        {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = ecipher.doFinal(utf8);
            return String.valueOf(Base64Coder.encode(enc));

        } catch (javax.crypto.BadPaddingException e)
        {
        } catch (IllegalBlockSizeException e)
        {
        } catch (UnsupportedEncodingException e)
        {
        } catch (java.io.IOException e)
        {
        }
        return null;
    }

    public void decrypt(InputStream in, OutputStream out) throws IOException {
        try
        {
            in = new CipherInputStream(in, dcipher);
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0)
            {
                out.write(buf, 0, numRead);
            }
        } finally
        {
            if (out != null)
                out.close();

            if (in != null)
                in.close();
        }
    }

    public String decrypt(String str) {
        try
        {
            byte[] dec = Base64Coder.decode(str);
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");

        } catch (javax.crypto.BadPaddingException e)
        {
        } catch (IllegalBlockSizeException e)
        {
        } catch (UnsupportedEncodingException e)
        {
        } catch (java.io.IOException e)
        {
        }
        return null;
    }
}
